package com.clj.view;

import com.clj.entity.StudentDO_clj;
import com.clj.handler.UpdateStudentViewHandler_clj;
import com.clj.service.StudentService_clj;
import com.clj.service.impl.StudentServiceCljImpl_clj;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * Author: Harry Chen
 * Date: 2023/02/24 15:53
 * Description: 主页面的表格体
 */
public class UpdateStudentView_clj extends JDialog {
    JPanel jPanel = new JPanel(new FlowLayout(1, 10, 20));
    JLabel idLabel = new JLabel("id:", 4);
    JTextField idTxt = new JTextField();
    JLabel nameLabel = new JLabel("姓名:", 4);
    JTextField nameTxt = new JTextField();
    JLabel noLabel = new JLabel("学号:", 4);
    JTextField noTxt = new JTextField();
    JLabel homeLabel = new JLabel("住址:", 4);
    JTextField homeTxt = new JTextField();
    JLabel cnLabel = new JLabel("语文成绩:", 4);
    JTextField cnTxt = new JTextField();
    JLabel mathLabel = new JLabel("数学成绩:", 4);
    JTextField mathTxt = new JTextField();
    JLabel enLabel = new JLabel("英语成绩:", 4);
    JTextField enTxt = new JTextField();
    JButton updateBtn = new JButton("修改");
    UpdateStudentViewHandler_clj updateStudentViewHandlerClj;

    public UpdateStudentView_clj(MainView_clj mainViewClj, int selectedStudentId) {
        super(mainViewClj, "修改学生", true);

        this.updateStudentViewHandlerClj = new UpdateStudentViewHandler_clj(this, mainViewClj);

        StudentService_clj studentServiceClj = new StudentServiceCljImpl_clj();
        StudentDO_clj selectedStu = studentServiceClj.getById(selectedStudentId);
        this.idLabel.setPreferredSize(new Dimension(80, 30));
        this.jPanel.add(this.idLabel);
        this.idTxt.setPreferredSize(new Dimension(200, 30));
        this.idTxt.setText(String.valueOf(selectedStu.getId()));

        this.idTxt.setEnabled(false);
        this.jPanel.add(this.idTxt);

        this.nameLabel.setPreferredSize(new Dimension(80, 30));
        this.jPanel.add(this.nameLabel);
        this.nameTxt.setPreferredSize(new Dimension(200, 30));
        this.nameTxt.setText(selectedStu.get姓名());
        this.jPanel.add(this.nameTxt);

        this.noLabel.setPreferredSize(new Dimension(80, 30));
        this.jPanel.add(this.noLabel);
        this.noTxt.setPreferredSize(new Dimension(200, 30));
        this.noTxt.setText(selectedStu.get学号());
        this.jPanel.add(this.noTxt);

        this.homeLabel.setPreferredSize(new Dimension(80, 30));
        this.jPanel.add(this.homeLabel);
        this.homeTxt.setText(selectedStu.get住址());
        this.homeTxt.setPreferredSize(new Dimension(200, 30));
        this.jPanel.add(this.homeTxt);

        this.cnLabel.setPreferredSize(new Dimension(80, 30));
        this.jPanel.add(this.cnLabel);
        this.cnTxt.setPreferredSize(new Dimension(200, 30));
        this.cnTxt.setText(String.valueOf(selectedStu.get语文()));
        this.jPanel.add(this.cnTxt);

        this.enLabel.setPreferredSize(new Dimension(80, 30));
        this.jPanel.add(this.enLabel);
        this.enTxt.setPreferredSize(new Dimension(200, 30));
        this.enTxt.setText(String.valueOf(selectedStu.get英语()));
        this.jPanel.add(this.enTxt);

        this.mathLabel.setPreferredSize(new Dimension(80, 30));
        this.jPanel.add(this.mathLabel);
        this.mathTxt.setText(String.valueOf(selectedStu.get数学()));
        this.mathTxt.setPreferredSize(new Dimension(200, 30));
        this.jPanel.add(this.mathTxt);

        this.updateBtn.addActionListener(this.updateStudentViewHandlerClj);
        this.jPanel.add(this.updateBtn);

        Container contentPane = getContentPane();
        contentPane.add(this.jPanel);

        setSize(350, 500);
        setLocationRelativeTo(null);

        setDefaultCloseOperation(2);
        setResizable(false);
        setVisible(true);
    }

    public StudentDO_clj buildUpdatedStudentDO() {
        StudentDO_clj studentDOClj = new StudentDO_clj();
        studentDOClj.setId(Integer.valueOf(this.idTxt.getText()).intValue());
        studentDOClj.set姓名(this.nameTxt.getText());
        studentDOClj.set学号(this.noTxt.getText());
        studentDOClj.set住址(this.homeTxt.getText());
        studentDOClj.set语文(Double.valueOf(this.cnTxt.getText()));
        studentDOClj.set英语(Double.valueOf(this.enTxt.getText()));
        studentDOClj.set数学(Double.valueOf(this.mathTxt.getText()));
        return studentDOClj;
    }
}
